package edu.ustc.iproject.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import edu.ustc.iproject.dao.NoteDAO;
import edu.ustc.iproject.bean.Employement;
import edu.ustc.iproject.bean.Note;
import edu.ustc.iproject.util.HibernateUtil;

public class NoteDAOImpl implements NoteDAO {

	public Note getSingleNoteById(int noteId) {

		Session session = HibernateUtil.openSession();
		Transaction tx = session.beginTransaction();
		Note note = null;
		try{
			note = (Note)session.get(Note.class, 1);
			tx.commit();
			
		}catch(Exception ex){
			System.out.println(ex);
			
			if(null != tx){
				tx.rollback();
			}
			
			
		}finally{
			
			HibernateUtil.close(session);
			
		}
		return note;
	}

	@SuppressWarnings("unchecked")
	public List<Note> listAllNotes() {
		
		Session session = HibernateUtil.openSession();
		Transaction tx = session.beginTransaction();
		
		List<Note> list_note = null;
		try{
			Query query = session.createQuery("from Note");
			list_note = query.list();
			tx.commit();
			
		}catch(Exception ex){
			System.out.println(ex);
			
			if(null != tx){
				tx.rollback();
			}
			
			
		}finally{
			
			HibernateUtil.close(session);
			
		}
		
		return list_note;
	}

	public void removeNote(int note_id) {

		Session session = HibernateUtil.openSession();
		Transaction tx = session.beginTransaction();
		
		try{
			Note note = (Note)session.get(Note.class, 1);
			session.delete(note);
			tx.commit();
			
		}catch(Exception ex){
			System.out.println(ex);
			
			if(null != tx){
				tx.rollback();
			}
			
			
		}finally{
			
			HibernateUtil.close(session);
			
		}
		
		
		
	}

	public void saveNote(Note note) {

		Session session = HibernateUtil.openSession();
		Transaction tx = session.beginTransaction();
		
		try{
			
			session.save(note);
			tx.commit();
			
		}catch(Exception ex){
			System.out.println("exception");
			
			if(null != tx){
				tx.rollback();
			}
			
			
		}finally{
			
			HibernateUtil.close(session);
			
		}
	}

	public void updateNote(Note note) {
		Session session = HibernateUtil.openSession();
		Transaction tx = session.beginTransaction();
		
try{
			
			session.update(note);
			tx.commit();
			
		}catch(Exception ex){
			System.out.println(ex);
			
			if(null != tx){
				tx.rollback();
			}
			
			
		}finally{
			
			HibernateUtil.close(session);
			
		}
		
	}

}
